# -*- coding: utf-8 -*-
'''
Created on 2010/4/28

@author: Daniel
'''
import os

from twisted.internet import reactor
from twisted.web import client

from parser_scoreboard import ParserScoreboard
from boxscore2 import BoxScore2

def getBoxSchedule(html):
    """
    """
    return ParserScoreboard(html)

def printScoreboard(p):
    """
    """
    for game in p.getGame():
        game.showGame()
    raw_input('press enter to continue...')
    os.system('cls')
    
    return p.getGidList()


def geBoxScore(gidList):
    """
    """
    for gid in gidList:
        yield BoxScore2(gid)

def printBoxScore(b):
    """
    """
    for bs in b:
        bs.run()
        bs.showBoxScore()
        raw_input('press enter to continue...')
        os.system('cls')
    reactor.stop()    

def printError(result):
    """
    """
    print result.getErrorMessage()
    reactor.stop()
    
def main(date):
    """main
    """
    url = 'http://sports.yahoo.com/mlb/scoreboard?d=%s' % date
    d = client.getPage(url)
    d.addCallback(getBoxSchedule)
    d.addErrback(printError)
    d.addCallback(printScoreboard)
    d.addErrback(printError)
    d.addCallback(geBoxScore)
    d.addErrback(printError)
    d.addCallback(printBoxScore)
    d.addErrback(printError)    
    reactor.run()    

if __name__ == '__main__':
    import sys
    if not len(sys.argv) == 2:
        print 'Usage: main.py date'
        sys.exit(1)
    date = sys.argv[1]
    main(date)
